package com.lml.jdk8.test05;

import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * @author Administrator
 * @date 2020/2/27 11:20
 * Description:
 */
public class DemoTest {

    @Test
    void fun1() {
        //方式1:Collection接口的方法
        Collection<String> col = new ArrayList<>();
        Stream<String> stream1 = col.stream();
        Stream<String> stream2 = col.parallelStream();

        //方式2:通过Arrays中的stream方法
        IntStream stream3 = Arrays.stream(new int[]{1, 2, 3});

        //方式3:Stream的of方法
        Stream<String> stream4 = Stream.of("aa", "bb");

        //方式4:Stream中的方法-创作无限流(结果是无限多个)
        Stream<Integer> stream5 = Stream.iterate(2, x -> x + 2);
        stream5.forEach(System.out::println);

        Stream<Double> stream6 = Stream.generate(Math::random);
        stream6.forEach(System.out::println);
    }

    @Test
    void test2() {
        System.out.println("asd" == "asd");
        //向上取整-->ceil 向下取整-->floor
        double ceil = Math.ceil(2.2);
        double floor = Math.floor(2.2);
        System.out.println("ceil = " + ceil);
        System.out.println("floor = " + floor);
    }

    @Test
    void test03() {
        System.out.println("myresult = " + sqrt(2, null));
        System.out.println("Math.sqrt = " + Math.sqrt(2));

        System.out.println("myresult = " + cbrt(-10, null));
        System.out.println("Math.sqrt = " + Math.cbrt(-10));
    }

    /**
     * 求一个数的平方根
     *
     * @param num     被求数
     * @param precise 精度
     * @return
     */
    public static double sqrt(Integer num, Double precise) {
        if (num < 0)
            throw new IllegalArgumentException("目标数不能为负数！");
        int i = num;
        for (; i > 0; i--) {
            if (i * i == num) {
                return i;
            }
            if (i * i < num) {
                break;
            }
        }
        double high = i + 1, low = i, pre = precise == null ? 1e-7 : precise;
        double middle = 0;
        while (high - low > pre) {
            middle = (high + low) / 2;
            if (middle * middle > num) {
                high = middle;
            } else {
                low = middle;
            }
        }
        return middle;
    }

    /**
     * 求一个数的立方根
     *
     * @param num     被求数
     * @param precise 精度
     * @return
     */
    public static double cbrt(Integer num, Double precise) {
        boolean negative = false;
        if (num < 0) {
            num = -num;
            negative = true;
        }
        int i = num;
        for (; i > 0; i--) {
            if (i * i * i == num) {
                return i;
            }
            if (i * i * i < num) {
                break;
            }
        }
        double high = i + 1, low = i, pre = precise == null ? 1e-7 : precise;
        double middle = 0;
        while (high - low > pre) {
            middle = (high + low) / 2;
            if (middle * middle * middle > num) {
                high = middle;
            } else {
                low = middle;
            }
        }
        return negative ? -middle : middle;
    }
}
